AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )


util.PrecacheSound( "explode_9" )
--util.PrecacheSound( "NPC_SScanner.Gib" )
util.PrecacheSound( "ambient/levels/labs/electric_explosion4.wav" )
util.PrecacheSound( "ambient/levels/labs/electric_explosion3.wav" )
util.PrecacheSound( "ambient/levels/labs/electric_explosion1.wav" )
--util.PrecacheSound( "weapons/mortar/mortar_explode3.wav" )
util.PrecacheSound( "ambient/explosions/exp2.wav" )
--util.PrecacheSound( "PortableThumper.ThumpSound" )
util.PrecacheSound( "k_lab.ambient_powergenerators" )
util.PrecacheSound( "ambient/machines/thumper_startup1.wav" )
util.PrecacheSound( "coast.siren_citizen" )
util.PrecacheSound( "common/warning.wav" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32
local Energy_Increment = 250
local Coolant_Increment = 25
local Hydrogen_Increment = 25
local HasFuel = false --does it have a hydrogen source?

function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	self.val1 = 0
	self.val2 = 0
	self.overdrive = 0
	self.overdrivefactor = 0 --will  be the same as overdrive but I'm to lazy to modify it
	self.damaged = 0
	
	if not (WireAddon == nil) then self.Inputs = Wire_CreateInputs(self.Entity, { "On", "Overdrive" }) end
	if not (WireAddon == nil) then self.Outputs = Wire_CreateOutputs(self.Entity, { "Out" }) end

	self.Entity:SetColor( 10, 96, 255, 255 )
	self.timer = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(3000)
		
	end
end

function ENT:Setup()
	self:TriggerInput("On", 0)
end

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		if (value ~= 0) then
			if ( self.Active == 0 ) then
				self.Active = 1
				self.Entity:EmitSound( "k_lab.ambient_powergenerators" )
				self.Entity:EmitSound( "ambient/machines/thumper_startup1.wav" )
				Wire_TriggerOutput(self.Entity, "Out", self.Active)
			end
		else
			if ( self.Active == 1 ) then
				self.Active = 0
				Wire_TriggerOutput(self.Entity, "Out", self.Active)
			end
		end
	elseif (iname == "Overdrive") then
		if (value > 0) then
			self.overdrive = 1
			self.Entity:EmitSound( "k_lab.ambient_powergenerators" )
			self.Entity:EmitSound( "ambient/machines/thumper_startup1.wav" )
			self.overdrivefactor = value
		else
			self.overdrive = 0
			self.overdrivefactor = 0
		end
	end
end

function ENT:Use()
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
	end
end

function ENT:Repair()
	self.Entity:SetColor( 10, 96, 255, 255 )
	self.health = self.max_health
	self.damaged = 0
	self.critical = 0
	self.Entity:StopSound( "coast.siren_citizen" )
end

function ENT:Destruct()
		local function Func1(Entity)
			if Entity:IsValid() then
				local Effect = EffectData()
					Effect:SetOrigin(Entity:GetPos() + Vector( math.random(-60, 60), math.random(-60, 60), math.random(-60, 60) ))
					Effect:SetScale(1)
					Effect:SetMagnitude(25)
					util.Effect("Explosion", Effect, true, true)
			end
		end
		local function Func2(Entity)
			if Entity:IsValid() then
				local Effect = EffectData()
					Effect:SetOrigin(Entity:GetPos())
					Effect:SetScale(3)
					Effect:SetMagnitude(100)
					util.Effect("Explosion", Effect, true, true)

					Entity:Remove()
			end
		end
		timer.Simple(1, Func1, self.Entity)
		timer.Simple(1.2, Func1, self.Entity)
		timer.Simple(2, Func1, self.Entity)
		timer.Simple(2, Func2, self.Entity)
		self.Entity:Remove()
end

function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
	self.Entity:StopSound( "k_lab.ambient_powergenerators" )
	self.Entity:StopSound( "coast.siren_citizen" )
	
end


function ENT:Output()
	return 1
end

function ENT:Extract_Energy()
	local inc = Energy_Increment
	local hyd = Hydrogen_Increment
	if ( self.overdrive > 0 ) then
		inc = math.Round(inc * self.overdrivefactor)
		if self.overdrivefactor > 3 then
			self:Destruct()
		else
			if self.overdrive > 1 then
				DamageLS(self, math.random(0, math.Round(self.overdrivefactor*3)))
			end
		end
	end
	
	local QuickHack = self.overdrive --just to make sure it uses resources
	if (QuickHack == 0) then
		QuickHack = 1 --make it run on standered
	end
	
	if HasFuel then
		if (inc > 0) then
			RD_SupplyResource(self, "energy", inc)
		end
	end
	
	--coolant check
	if (RD_GetResourceAmount(self, "coolant") <=  math.Round(QuickHack*Coolant_Increment)) then
		if (self.critical == 0) then self.Entity:EmitSound( "common/warning.wav" ) end
	else
		RD_ConsumeResource(self, "coolant", math.Round(QuickHack*Coolant_Increment))
	end
	
	--fuel (hydrogen) check
	if (RD_GetResourceAmount(self, "hydrogen") <= math.Round(QuickHack*Hydrogen_Increment)) then
		if (self.critical == 0) then self.Entity:EmitSound( "common/warning.wav" ) end
		HasFuel = false
	else
		RD_ConsumeResource(self, "hydrogen", math.Round(QuickHack*Hydrogen_Increment))
		HasFuel = true
	end
	

	return
end



function ENT:Think()
	if ( self.Active == 0 ) then
		self:SetOverlayText( "Fusion Reactor\n(OFF)\nCoolant: " .. self.val1.."\n Hydrogen: "..self.val2)
	else
		if (self.overdrive > 0) then
			self:SetOverlayText( "Fusion Reactor\n(ON)\n(OVERDRIVE)\nCoolant: " .. self.val1.."\n Hydrogen: "..self.val2)
		else
			self:SetOverlayText( "Fusion Reactor\n(ON)\nCoolant: " .. self.val1.."\n Hydrogen: "..self.val2)
		end
	end
	if ( self.timer > CurTime() ) then return end
	if (self.Active == 1) then
		self:Extract_Energy()
	end
	self.val1 = RD_GetResourceAmount(self, "coolant")
	self.val2 = RD_GetResourceAmount(self, "hydrogen")
	self.timer = CurTime() +  1
end


function ENT:AcceptInput(name,activator,caller)
	if name == "Use" and caller:IsPlayer() and caller:KeyDownLast(IN_USE) == false then
		if ( self.Active == 0 ) then
			self.Active = 1
			self.Entity:EmitSound( "k_lab.ambient_powergenerators" )
			self.Entity:EmitSound( "ambient/machines/thumper_startup1.wav" )
			
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", 1) end
		else
			self.Active = 0
			self.Entity:StopSound( "k_lab.ambient_powergenerators" )
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", 0) end
		end
	end
end


function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
	if (WireAddon == 1) then
		local DupeInfo = Wire_BuildDupeInfo(self.Entity)
		if DupeInfo then
			duplicator.StoreEntityModifier( self.Entity, "WireDupeInfo", DupeInfo )
		end
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
	if (WireAddon == 1) then
		if (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
			Wire_ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
		end
	end
end
